<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="分类名称" prop="catName">
        <el-input v-model="queryParams.catName" placeholder="请输入分类名称" clearable size="small" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="是否展示" prop="isShow">
        <el-select v-model="queryParams.isShow" placeholder="请选择是否展示" clearable size="small">
          <el-option v-for="dict in isShowOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['category:category:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['category:category:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['category:category:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['category:category:export']">导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" border stripe :data="categoryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="分类名称" align="center" prop="catName" />
      <!-- <el-table-column label="分类关联父ID" align="center" prop="parentId" /> -->
      <el-table-column label="分类排序" align="center" prop="sort" />
      <el-table-column label="是否展示" align="center" prop="isShow" :formatter="isShowFormat" />
      <el-table-column label="分类小图标" align="center" prop="iconMiniImg" width="90">
        <template slot-scope="scope">
          <img v-if="scope.row.iconMiniImg" :src="scope.row.iconMiniImg" alt="" width="100%" height="100%" />
        </template>
      </el-table-column>
      <el-table-column label="分类图片" align="center" prop="iconImg" width="107">
        <template slot-scope="scope">
          <img v-if="scope.row.iconImg" :src="scope.row.iconImg" alt="" width="100%" height="100%" />
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['category:category:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['category:category:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script>
import { listCategory, delCategory, exportCategory } from '@/api/category/category'

export default {
  name: 'Category',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 商品分类表格数据
      categoryList: [],
      // 是否展示字典
      isShowOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        catName: undefined,
        isShow: undefined
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('is_active').then(response => {
      this.isShowOptions = response.data
    })
  },
  methods: {
    /** 查询商品分类列表 */
    getList() {
      this.loading = true
      listCategory(this.queryParams).then(response => {
        this.categoryList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 是否展示字典翻译
    isShowFormat(row, column) {
      return this.selectDictLabel(this.isShowOptions, row.isShow)
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push('/LSY/LJCLXLX/category/edit/new')
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push('/LSY/LJCLXLX/category/edit/' + (row.id || this.ids))
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除商品分类编号为"' + ids + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return delCategory(ids)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(function() {})
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有商品分类数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          return exportCategory(queryParams)
        })
        .then(response => {
          this.download(response.msg)
        })
        .catch(function() {})
    }
  }
}
</script>
<style>
.pagination-container .el-pagination__total,
.pagination-container .el-pagination__jump {
  color: #606266;
}
</style>
